library(dplyr)
library(ggplot2)
library(bibliometrix) 
library(Cairo)
library(FactoMineR)
library(ca)
library(ggrepel)
library(dplyr)
library(xlsx)

ismy <- file.exists("F:/rproject/wosexporttest/")
if(ismy){
  setwd("F:/rproject/wosexporttest")
}else{
  setwd('/sas/pubmedr/gcr/20231116')
}
getwd()
# 部分源码不想写了 直接在工具类里面赋值进去
source('sourcer/utils.R')

# https://www.bibliometrix.org/vignettes/Introduction_to_bibliometrix.html
# bibliometrix
# 2023年12月7日13:26:00  测试wos 导出带参文进行画图 测试K-means clusterin 转成java测试 学习
file<- c('savedrecsre1.txt')

M <- convert2df(file = file, dbsource = "wos", format = "plaintext")

# 生成 他们的id与PM对应
rownames(M) <- paste(rownames(M),M$PM,sep = ' PMID:')
try({
  CS <- conceptualStructure(M,field="ID", method="CA", minDegree = 3, clust=5, stemming=FALSE, labelsize=10, documents = 2)
})


# 
# 下面是源码拿出来测试用的 
# clustering 分组 根据相似的特性把一堆数据进行分组
# 理解 2023年12月7日10:00:17 这个上面的列相当于维度进行展示 也就是每个词左侧对应的文章 0 代表有 1 代表没有 生成一张表 列越多代表着词越多
#最左侧标号相当于是文章id 然后的每一列代表词 如本目录下面的qq截图的显示那样 选取了大于4的维度  参数 minDegree  默认是2 也就是出现次数大于等于2次单词才才计入统计
# getwd() 首先是聚类的个数也就是上面方法conceptualStructure的 clust 参数 如果选择 auto 默认是2-8之间
# 目前显示的是期刊

quali = NULL
quanti = NULL
quali.supp = NULL
quanti.supp = NULL
synonyms = NULL
remove.terms = NULL
labelsize=10
#次数最低出现次数纳入计算
minDegree = 3
#方法 ca
method = "CA"
clust = 5
# 保留最大集群数量 默认是5 
k.max = 5
# 分区最大显示文章数量
documents = 2
# 自定义源码 factorial 而不能用base 包下默认的

# cbPalette <- colorlist()
SUPP = data.frame(M[, quanti.supp])
names(SUPP) = names(M)[quanti.supp]
row.names(SUPP) = tolower(row.names(M))

binary = FALSE
if (method == "MCA") {
  binary = TRUE
}

CW <- cocMatrix(M, Field = "ID", type = "matrix", sep = ";", 
                binary = binary, remove.terms = remove.terms, synonyms = synonyms)

rownames(CW) <- paste(rownames(CW),M$PM,sep = ' PMID:')

CW = CW[, colSums(CW) >= minDegree]
CW = CW[, !(colnames(CW) %in% "NA")]
CW = CW[rowSums(CW) > 0, ]
cwdframFilter <- as.data.frame(CW)

# write.csv(M[,c('UT','TI','ID')],file = '20231206.csv')
 
 colnames(CW) = tolower(colnames(CW))
 rownames(CW) = tolower(rownames(CW))
 p = dim(CW)[2]
 # res.mca <- CA(CW, quanti.sup=quanti, quali.sup=quali, ncp=2, graph=FALSE)
 results <- factorial(CW, method = method, quanti = quanti, quali = quali)
 res.mca <- results$res.mca
 df <- results$df
 docCoord <- results$docCoord
 df_quali <- results$df_quali
 df_quanti <- results$df_quanti
 
 ### Total Citations of documents 全部文档被引次数
 if ("TC" %in% names(M) & method!="MDS"){docCoord$TC=as.numeric(M[toupper(rownames(docCoord)),"TC"])}
 
 # Selection of optimal number of clusters (gap statistics) 选择最优簇数(差距统计)
 #a=fviz_nbclust((df), kmeans, method = "gap_stat",k.max=k.max)['data']$data$y
 km.res=hclust(dist(df),method="average")
 if (clust=="auto"){
   clust=min((length(km.res$height)-which.max(diff(km.res$height))+1),k.max)
 }else{clust=max(1,min(as.numeric(clust),k.max))}

 km.res$data=df
 km.res$cluster=cutree(km.res,k=clust)
 km.res$data.clust=cbind(km.res$data,km.res$cluster)
 names(km.res$data.clust)[3]="clust"
 centers<- km.res$data.clust %>% group_by(.data$clust) %>% 
   summarise("Dim.1"=mean(.data$Dim.1),"Dim.2"=mean(.data$Dim.2)) %>% 
   as.data.frame()
 
 km.res$centers=centers[,c(2,3,1)]
 data("logo",envir=environment())
 logo <- grid::rasterGrob(logo,interpolate = TRUE)
 
 df_clust <- km.res$data.clust %>% 
   mutate(shape = "1",
          label = row.names(.)) %>% 
   bind_rows(km.res$centers %>% mutate(shape = "0", label="")) %>% 
   mutate(color = colorlist()[.data$clust])
 
 hull_data <- 
   df_clust %>%
   group_by(.data$clust) %>% 
   slice(chull(.data$Dim.1, .data$Dim.2))
 
 hull_data <- hull_data %>%
   bind_rows(
     hull_data %>% group_by(clust) %>% slice_head(n=1)
   ) %>%
   mutate(id = row_number()) %>%
   arrange(.data$clust,.data$id)
 
 size <- labelsize
 # 把每个点所在的文章集合拿过来 gcr 2023年12月8日15:35:58
  df_clust <- dplyr::mutate(df_clust,keys = row.names(df_clust))
  CWframe<- as.data.frame(CW)
  CWframe <- dplyr::mutate(CWframe,keys = row.names(CWframe))
 # indexKey <-(colnames(CWframe))
 # indexKey<-indexKey[1:(length(indexKey)-1)]
 rows<- as.data.frame(rownames(CWframe))
 colnames(rows) <- 'arti'
 rownames(CWframe) <- NULL
 colnames(CWframe)
 # data.frame(CWframe,row.names=1)
 CWframecandr<-t(CWframe)
 colnames(CWframecandr) <- CWframe[,'keys']
 
 # 删除多余行
 CWframecandrdel <- CWframecandr[-dim(CWframecandr)[1],]
 CWframecandrdel<-as.data.frame(CWframecandrdel)
 CWframecandrdel <- dplyr::mutate(CWframecandrdel,keys = row.names(CWframecandrdel))

 rownames(CWframecandrdel) <- NULL
 alldata1 <- dplyr::left_join(df_clust,CWframecandrdel,by = 'keys')
 
 # 测试一次拿到所有字段 2023年12月9日09:31:23 
 # CWframecandrdelTest<- CWframecandrdel[CWframecandrdel$keys=='fibroblasts',1]
 # CWframecandrdelt<- t(CWframecandrdel)
 # CWframecandrdelt<- as.data.frame(CWframecandrdelt)
 # colnames(CWframecandrdelt) <- CWframecandrdelt[dim(CWframecandrdelt)[1],]
 # CWframecandrdelt<- CWframecandrdelt[-dim(CWframecandrdelt)[1],]
 # # 不搞了 ,太麻烦了 生成本地 java进行模拟 2023年12月9日09:44:10 
 # write.ex(alldata1,file = 'alldata1.csv')
 # 指定x待写入数据，file生成的文件名，row.names为false则不生成行名，指定sheet工作表名为Sheet1
 write.xlsx(alldata1, file = "alldata1.xlsx", row.names = FALSE, sheetName = "Sheet1")
 

 
  # 循环数据搞不定 才用行转列 进行拼接吧
 # for (variable in indexKey) {
 #   print(variable)
 #   aa<-as.data.frame(CWframe[,variable])
 #   colnames(aa) <- variable
 #   dim(aa[aa$variable==1,])
 #   aa$flag < as.numeric(aa$flag )
 #   aa2<-cbind(rows,aa)
 #   
 # }
 # for (variable in asss) {
 #   print(variable)
 #   aa<-as.data.frame(CWframe[,variable])
 #   dim(aa)
 #   colnames(aa) <- 'flag'
 #   aa$flag < as.numeric(aa$flag )
 #   aa2<- dplyr::cross_join(rows,aa)
 #    print(dim(aa2))
 #    print(("+++++++++++++++++++++++++++++"))
 # }
 print(("+++++++++++++++++++++++++++++"))
 
 # 画图给
 # b <- ggplot(df_clust, aes(x=.data$Dim.1, y=.data$Dim.2, shape=.data$shape, color=.data$color)) +
 #   geom_point() + 
 #   geom_polygon(data = hull_data,
 #                aes(fill = .data$color,
 #                    colour = .data$color),
 #                alpha = 0.3,
 #                show.legend = FALSE) +
 #   ggrepel::geom_text_repel(aes(label=.data$label)) +
 #   theme_minimal()+
 #   labs(title= paste("Conceptual Structure Map - method: ",method,collapse="",sep="")) +
 #   geom_hline(yintercept=0, linetype="dashed", color = adjustcolor("grey40",alpha.f = 0.7))+
 #   geom_vline(xintercept=0, linetype="dashed", color = adjustcolor("grey40",alpha.f = 0.7))+
 #   theme(
 #     text = element_text(size=size),
 #     axis.title=element_text(size=size,face="bold"),
 #     plot.title=element_text(size=size+1,face="bold"),
 #     panel.background = element_rect(fill = "white", colour = "white"),
 #     axis.line.x = element_line(color="black",linewidth=0.5),
 #     axis.line.y = element_line(color="black",linewidth=0.5),
 #     panel.grid.major = element_blank(),
 #     panel.grid.minor = element_blank())
 # 
 # b 
 # 这个是关键词聚类结束
 # 至此 这里就实现图一显示 需要合并下数据
 
 # Factorial map of the documents with the highest contributes
 # 贡献值最高的文档的阶乘映射
 ## Factorial map of most contributing documents
 if (documents>dim(docCoord)[1]){
   documents=dim(docCoord)[1]
 }
 centers = data.frame(dim1 = km.res$centers[, 1], dim2 = km.res$centers[, 2])
 cbPalette <- colorlist()#c(brewer.pal(9, 'Set1')[-6], brewer.pal(8, 'Set2')[-7], brewer.pal(12, 'Paired')[-11],brewer.pal(12, 'Set3')[-c(2,8,12)])
 centers$color = cbPalette[1:dim(centers)[1]]
 # 上色
 row.names(centers) = paste("cluster", as.character(1:dim(centers)[1]), sep = "")
 A = euclDist(docCoord[, 1:2], centers)
 docCoord$Cluster = A$color
 A$color = cbPalette[A$color]
 A$contrib <- docCoord$contrib
 A <- A %>% mutate(names = row.names(A)) %>% group_by(.data$color) %>% 
   top_n(n = documents, wt = .data$contrib) %>% select(!"contrib") %>% 
   as.data.frame()

 write.xlsx(alldata1, file = "alldata1.xlsx", row.names = FALSE, sheetName = "Sheet1")
 #下面不需要这个搞了 原来就有了 还是需要组合数据
 row.names(A) <- A$names
 A <- A[, -4]
 names(centers) = names(A)
 allSize<- dim(A)[1]
 A = rbind(A, centers)
 x = A$dim1
 y = A$dim2
 A[, 4] = row.names(A)
 names(A)[4] = "nomi"
 
 # 添加一列分组进行合并到一起 测试
 centersMy <- dplyr::mutate(centers,cluster = row.names(centers))
 colnames(centersMy)<- paste('center', colnames(centersMy))
 paramA<- A
 colnames(A)[3] <-c('center color')
 aceters <- dplyr::left_join(A,centersMy,by = 'center color')
 # aceters <- aceters[c(1:allSize),]
 
 # 至此 这个A 应该就是我需要的数据了 可以存在本地 后续再看
 write.xlsx(aceters,file = 'Factorial map of the documents.xlsx')

 
 # df_all = rbind(as.matrix(df), as.matrix(A[, 1:2]))
 # rangex = c(min(df_all[, 1]), max(df_all[, 1]))
 # rangey = c(min(df_all[, 2]), max(df_all[, 2]))

 
#  
#  b_doc <- ggplot(aes(x = .data$dim1, y = .data$dim2, 
#                      label = .data$nomi), data = A) + geom_point(size = 2, 
#                                                                  color = A$color) + labs(title = "Factorial map of the documents with the highest contributes") + 
#    geom_label_repel(box.padding = unit(0.5, "lines"), 
#                     size = (log(labelsize * 3)), fontface = "bold", 
#                     fill = adjustcolor(A$color, alpha.f = 0.6), 
#                     color = "white", segment.alpha = 0.5, segment.color = "gray") + 
#    scale_x_continuous(limits = rangex, breaks = seq(round(rangex[1]), 
#                                                     round(rangex[2]), 1)) + scale_y_continuous(limits = rangey, 
#                                                                                                breaks = seq(round(rangey[1]), round(rangey[2]), 
#                                                                                                             1)) + geom_hline(yintercept = 0, linetype = "dashed", 
#                                                                                                                              color = adjustcolor("grey40", alpha.f = 0.7)) + 
#    geom_vline(xintercept = 0, linetype = "dashed", 
#               color = adjustcolor("grey40", alpha.f = 0.7)) + 
#    theme(text = element_text(size = labelsize), axis.title = element_text(size = labelsize, 
#                                                                           face = "bold"), plot.title = element_text(size = labelsize + 
#                                                                                                                       1, face = "bold"), panel.background = element_rect(fill = "white", 
#                                                                                                                                                                          colour = "white"), axis.line.x = element_line(color = "black", 
#                                                                                                                                                                                                                        size = 0.5), axis.line.y = element_line(color = "black", 
#                                                                                                                                                                                                                                                                size = 0.5), panel.grid.major = element_blank(), 
#          panel.grid.minor = element_blank())
#  
# b_doc
 # CS$graph_documents_TC
print('全部结束')




